package com.leantech.android.browser.dialog

import android.content.Context
import com.leantech.android.browser.databinding.LayoutMenuFontSizeBinding
import com.leantech.android.browser.utils.onThrottleClick
import com.jaygoo.widget.OnRangeChangedListener
import com.jaygoo.widget.RangeSeekBar
import com.qmuiteam.qmui.kotlin.dip
import com.qmuiteam.qmui.skin.QMUISkinManager
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet

class FontSizeBottomSheet(context: Context,
                          onProgressChanged: (Int) -> Unit = {}) :
    QMUIBottomSheet(context) {

    private val binding: LayoutMenuFontSizeBinding =
        LayoutMenuFontSizeBinding.inflate(layoutInflater)

    init {
        addContentView(binding.root)
        setFitNav(true)
        setRadius(context.dip(24))
        setSkinManager(QMUISkinManager.defaultInstance(context))

        binding.btnFontDecrease.onThrottleClick {
            val currentProgress = binding.fontSizeSeekBar.leftSeekBar.progress
            binding.fontSizeSeekBar.setProgress(currentProgress - 20)
        }

        binding.btnFontIncrease.onThrottleClick {
            val currentProgress = binding.fontSizeSeekBar.leftSeekBar.progress
            binding.fontSizeSeekBar.setProgress(currentProgress + 20)
        }

        binding.fontSizeSeekBar.setOnRangeChangedListener(object : OnRangeChangedListener {
            override fun onRangeChanged(
                view: RangeSeekBar?, leftValue: Float, rightValue: Float, isFromUser: Boolean
            ) {
                val fontSize = leftValue.toInt()
                if(fontSize in 80 .. 160 step 20){
                    onProgressChanged(leftValue.toInt())
                }
            }

            override fun onStartTrackingTouch(view: RangeSeekBar?, isLeft: Boolean) {

            }

            override fun onStopTrackingTouch(view: RangeSeekBar?, isLeft: Boolean) {

            }
        })

        binding.fontSizeSeekBar.setProgress(100f)

    }

    fun updateProgress(fontSize: Int) {
        binding.fontSizeSeekBar.setProgress(fontSize.toFloat())
    }

}